<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class media_env_airport_manage extends CI_Controller {


	public function __construct() {
		session_start();
		parent::__construct();
		$this->load->helper("common");
	}

	public function  index(){
		$this->env_airport();
	}
	public function env_airport(){


		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		$data['query_airport_env']	=false;
		if ($this->right_manage->check_right("query_airport_env")) {
			$data['query_airport_env']	=true;
		}
		$data['delete_airport_env']	=false;
		if ($this->right_manage->check_right("delete_airport_env")) {
			$data['delete_airport_env']=true;
		}
		$data['add_airport_env']	=false;
		if ($this->right_manage->check_right("add_airport_env")) {
			$data['add_airport_env']	=true;
		}
		$data['update_airport_env']	=false;
		if ($this->right_manage->check_right("update_airport_env")) {
			$data['update_airport_env']	=true;
		}

		$data['assign_excellent_media']	=false;
		if ($this->right_manage->check_right("assign_excellent_media")) {
			$data['assign_excellent_media']	=true;
		}

		$search['province'] 	= $this->input->get("province");
		$search['city']			= $this->input->get("city");
		$search['region'] 		= $this->input->get("region");
		$search['keyword'] 		= $this->input->get("keyword");


		$this->load->library("env/env_airport");
		$data['env_airport_list'] 	=$this->env_airport->query_airport_list($_SESSION['user_id'],$search);
		$data['num']				=$this->env_airport->query_airport_num($_SESSION['user_id'],$search);



		$this->load->library("sys/region");
		$data['province_list']	= $this->region->query_user_top_regions($_SESSION['user_id']);
		$data['city_list'] =null;
		$data['region_list'] =null;
		if($search['province']){
			$data['city_list'] =$this->region->get_regions($search['province']);
		}
		if($search['city']){
			$data['region_list'] =$this->region->get_regions($search['city']);
		}

		$data['search']=$search;
		$search_url =format_search_url($search);
		$this->load->library("tools/pagination");
		$data['page_link']=$this->pagination->get_pagination(base_url()."index.php/media_env_airport_manage/env_airport?".$search_url,$data['num']);

		$this->load->view("media/env_airport/env_airport_main",$data);
	}
	public function add_env_airport(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();


		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_airport_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->query_user_top_regions($_SESSION['user_id']);

		$this->load->view("media/env_airport/add_env_airport",$data);
	}
	public function save_env_airport(){

		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_airport_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}

		$airport['airport_name'] =trim($this->input->post("airport_name"));
		$airport['province'] =trim($this->input->post("province"));
		$airport['city'] =trim($this->input->post("city"));
		$airport['region'] =trim($this->input->post("region"));

		$airport['create_by'] =$_SESSION['user_name'];
		$airport['create_time'] =date("Y-m-d H:i:s");
		$this->load->library("env/env_airport");

		$result =$this->env_airport->save_env_airport($airport);
		$this->load->helper("common");
		echo JSON($result);
	}




	public function edit_env_airport(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_airport_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}


		$airport_id =$this->input->get("airport_id");
		$this->load->library("env/env_airport");
		$data['env_airport_info']=$this->env_airport->query_env_airport($airport_id);


		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->query_user_top_regions($_SESSION['user_id']);
		$data['second_regions'] =null;
		$data['last_regions']	=null;
		if($data['env_airport_info']['city']){
			$data['second_regions'] =$this->region->get_regions($data['env_airport_info']['province']);
		}
		if($data['env_airport_info']['region']){
			$data['last_regions'] =$this->region->get_regions($data['env_airport_info']['city']);
		}
		$this->load->view("media/env_airport/edit_env_airport",$data);
	}

	public function update_env_airport(){

		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}


		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_airport_env",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}

		$airport_id =$this->input->post('airport_id');

		$airport['airport_name'] =trim($this->input->post("airport_name"));
		$airport['province'] =trim($this->input->post("province"));
		$airport['city'] =trim($this->input->post("city"));
		$airport['region'] =trim($this->input->post("region"));

		$airport['modify_by'] =$_SESSION['user_name'];
		$airport['modify_time'] =date("Y-m-d H:i:s");

		$this->load->library("env/env_airport");
		$result =$this->env_airport->update_env_airport($airport,$airport_id);
		$this->load->helper("common");
		echo JSON($result);
	}

	public function delete_env_airport() {
		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();


		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("delete_airport_env",$_SESSION['user_id'])) {
			$this->load->library("common/user_menu");
			$data['user_menu']=$this->user_menu->get_user_menu();
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("env/env_airport");
		$airport_id =$this->input->get('airport_id');
		$this->env_airport->delete_env_airport($airport_id);
		echo "<script>history.go(-1)</script>";
	}

}